/*
 * @Date: 2023-03-25 16:00:52
 * @LastEditors: Zireael 1193875164@qq.com
 * @LastEditTime: 2023-03-26 09:51:48
 * @FilePath: \A19\frontend\src\utils\excel.ts
 */
import { ResGetAllHWInfo } from "@/shared/protocols/Get/PtlGetAllHWInfo";
import ExcelJS from "exceljs";
import { safeStu } from '../shared/protocols/Get/PtlGetAllHWInfo';

export class Excel {
  static exprotExcel(
    name: string,
    datas: {
      info: {
        name: string;
        // scores:number[]
      }[];
      students:(string | number)[][]
    },
    loadingInstance:{close:Function}
  ) {

    console.log(datas);

    const workbook = new ExcelJS.Workbook();
    workbook.creator = "teahcer";
    workbook.created = new Date();

    const workSheet = workbook.addWorksheet("Sheet1", {
      properties: {  },
    });

    // workSheet.columns = [
    //     {header:'',key:'index',width:5,},
    //     {header:'姓名',key:'name',width:10,},
    //     {header:'学号',key:'number',width:10,},
    //     ...datas.info.map((test,index) =>{
    //         return {
    //             header:test.name,
    //             key:index.toString(),
    //             width:10,
    //             values:[10,9,28]
    //         }
    //     })
    // ]

    // workSheet.getColumn('index').values =  [,,...datas.students.map((_,index) => index )]
    workSheet.addTable({
        name: 'MyTable',
        ref: 'A1',
        headerRow: true,
        columns: [
          {name: '序号'},
          {name: '姓名'},
          {name: '学号'},
          ...datas.info.map((test,index) =>{
            return {
                name:test.name
            }
          })
        ],
        rows: datas.students,
      });


    workbook.xlsx.writeBuffer().then((data) => {
      let blob = new Blob([data], {
        type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
      });
      loadingInstance.close()
      ElMessage.success('导出成功')
      const a = document.createElement("a");
      a.href = URL.createObjectURL(blob);
      a.download = name + ".xlsx";
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
      window.URL.revokeObjectURL(a.href);
    });
  }
}
